package com.company.service;

import com.company.dao.NewsDao;
import com.company.entity.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PageNewsService {
    @Autowired
    NewsDao newsDao;

    private static final int NUM = 5;


    public List<News> query(int pageNum) {
        Pageable pageable = new PageRequest(pageNum, NUM, Sort.Direction.DESC);
        return newsDao.findAll(pageable).getContent();
    }

    public List<News> querylast() {
        Pageable pageable = new PageRequest(0, NUM, Sort.Direction.DESC);
        return newsDao.findAll(pageable).getContent();
    }


    public int queryPageNum() {
        int total = newsDao.findAll().size();
        return total % NUM == 0 ? total / NUM : total / NUM + 1;
    }
}
